//    v6----- v5
//   /|      /|
//  v1------v0|
//  | |     | |
//  | |v7---|-|v4
//  |/      |/
//  v2------v3


rotate = 0
translateX = 0
translateY = 0
vertexList = 0
indexList = 0
colorList = 0
normalList = 0

function initData()
{
	vertexList = gl.createByteArray()
	indexList = gl.createByteArray()
	colorList = gl.createByteArray()
	normalList = gl.createByteArray()

	vertexList.writeFloat32(0.4)
	vertexList.writeFloat32(0.4)
	vertexList.writeFloat32(0.4)
	vertexList.writeFloat32(-0.4) 
	vertexList.writeFloat32(0.4)  
	vertexList.writeFloat32(0.4)
	vertexList.writeFloat32(-0.4) 
	vertexList.writeFloat32(-0.4) 
	vertexList.writeFloat32(0.4)
	vertexList.writeFloat32(0.4)  
	vertexList.writeFloat32(-0.4) 
	vertexList.writeFloat32(0.4)
	vertexList.writeFloat32(0.4)  
	vertexList.writeFloat32(-0.4) 
	vertexList.writeFloat32(-0.4)
	vertexList.writeFloat32(0.4)  
	vertexList.writeFloat32(0.4)  
	vertexList.writeFloat32(-0.4)
	vertexList.writeFloat32(-0.4) 
	vertexList.writeFloat32(0.4)  
	vertexList.writeFloat32(-0.4)
	vertexList.writeFloat32(-0.4) 
	vertexList.writeFloat32(-0.4) 
	vertexList.writeFloat32(-0.4)

	indexList.writeInt32(0) 
	indexList.writeInt32(1) 
	indexList.writeInt32(2) 
	indexList.writeInt32(3)
	indexList.writeInt32(0) 
	indexList.writeInt32(3) 
	indexList.writeInt32(4) 
	indexList.writeInt32(5)
	indexList.writeInt32(4) 
	indexList.writeInt32(5) 
	indexList.writeInt32(6) 
	indexList.writeInt32(7)
	indexList.writeInt32(1) 
	indexList.writeInt32(2) 
	indexList.writeInt32(7) 
	indexList.writeInt32(6)
	indexList.writeInt32(0) 
	indexList.writeInt32(1) 
	indexList.writeInt32(6) 
	indexList.writeInt32(5)
	indexList.writeInt32(2) 
	indexList.writeInt32(3) 
	indexList.writeInt32(4) 
	indexList.writeInt32(7)

	colorList.writeFloat32(1)  
	colorList.writeFloat32(0)  
	colorList.writeFloat32(0)
	colorList.writeFloat32(0) 
	colorList.writeFloat32(1)  
	colorList.writeFloat32(0)
	colorList.writeFloat32(0) 
	colorList.writeFloat32(0) 
	colorList.writeFloat32(1)
	colorList.writeFloat32(1)  
	colorList.writeFloat32(1) 
	colorList.writeFloat32(0)
	colorList.writeFloat32(1)  
	colorList.writeFloat32(0) 
	colorList.writeFloat32(1)
	colorList.writeFloat32(0)  
	colorList.writeFloat32(1)  
	colorList.writeFloat32(1)
	colorList.writeFloat32(1) 
	colorList.writeFloat32(1)  
	colorList.writeFloat32(1)
	colorList.writeFloat32(0) 
	colorList.writeFloat32(0) 
	colorList.writeFloat32(0)

	normalList.writeFloat32(1)  
	normalList.writeFloat32(0)  
	normalList.writeFloat32(0)
	normalList.writeFloat32(0) 
	normalList.writeFloat32(1)  
	normalList.writeFloat32(0)
	normalList.writeFloat32(0) 
	normalList.writeFloat32(0) 
	normalList.writeFloat32(1)
	normalList.writeFloat32(0)  
	normalList.writeFloat32(0) 
	normalList.writeFloat32(-1)
	normalList.writeFloat32(0)  
	normalList.writeFloat32(-1) 
	normalList.writeFloat32(0)
	normalList.writeFloat32(-1)  
	normalList.writeFloat32(0)  
	normalList.writeFloat32(0)
	normalList.writeFloat32(-1) 
	normalList.writeFloat32(1)  
	normalList.writeFloat32(0)
	normalList.writeFloat32(0) 
	normalList.writeFloat32(-1) 
	normalList.writeFloat32(-1)
}

function drawString(str)
{
	gl.glPushAttrib(gl.GL_LIGHTING_BIT + gl.GL_CURRENT_BIT)
	gl.glDisable(gl.GL_LIGHTING)
	var positionList = gl.createByteArray()
	positionList.writeFloat32(-0.6)  
	positionList.writeFloat32(-0.5)  
	positionList.writeFloat32(0.5)
	gl.glRasterPos3fv(positionList.getMemory())
	for(i=0; i < str.length; ++i) {
		gl.glutBitmapCharacter(gl.GLUT_BITMAP_TIMES_ROMAN_24, str.charCodeAt(i))
	}
	gl.glEnable(gl.GL_LIGHTING)
	gl.glPopAttrib()
}

function render()
{
	gl.glClear(gl.GL_COLOR_BUFFER_BIT + gl.GL_DEPTH_BUFFER_BIT + gl.GL_STENCIL_BUFFER_BIT)

	gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
	gl.glEnableClientState(gl.GL_COLOR_ARRAY)
	gl.glEnableClientState(gl.GL_NORMAL_ARRAY)
	    gl.glPushMatrix()
	gl.glTranslatef(translateX, translateY, 0)
	gl.glRotatef(rotate, 1, 0, 1)
	gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertexList.getMemory())
	gl.glColorPointer(3, gl.GL_FLOAT, 0, colorList.getMemory())
	gl.glNormalPointer(gl.GL_FLOAT, 0, normalList.getMemory())
	gl.glDrawElements(gl.GL_QUADS, 24, gl.GL_UNSIGNED_INT, indexList.getMemory())
	drawString("Javascript+OpenGL=Whoa!")
    gl.glPopMatrix()
	gl.glDisableClientState(gl.GL_VERTEX_ARRAY)
	gl.glDisableClientState(gl.GL_COLOR_ARRAY)
	gl.glDisableClientState(gl.GL_NORMAL_ARRAY)
	gl.glutSwapBuffers()
}

function reshape(w, h)
{
	gl.glViewport(0, 0, w, h)
	gl.glMatrixMode(gl.GL_MODELVIEW)
}

function specialKey(key, x, y)
{
	delta = 0.02
	if(key == gl.GLUT_KEY_LEFT)
	translateX = translateX - delta
	else if(key == gl.GLUT_KEY_RIGHT)
	translateX = translateX + delta
	else if(key == gl.GLUT_KEY_UP)
	translateY = translateY + delta
	else if(key == gl.GLUT_KEY_DOWN)
	translateY = translateY - delta
	
}

function keyboard(key, x, y)
{
	if(key == 27) exitDemo()
}

function timer(value)
{
	rotate = rotate + 1
	rotate = rotate % 360
	gl.glutPostRedisplay()
	gl.glutTimerFunc(value, timer, value)
}

function initLights()
{
	lightAmbient = gl.createByteArray()
	lightAmbient.writeFloat32(0.6) 
	lightAmbient.writeFloat32(0.6) 
	lightAmbient.writeFloat32(0.6) 
	lightAmbient.writeFloat32(1.0)

	lightDiffuse = gl.createByteArray()
	lightDiffuse.writeFloat32(0.7) 
	lightDiffuse.writeFloat32(0.7) 
	lightDiffuse.writeFloat32(0.7) 
	lightDiffuse.writeFloat32(1.0)

	lightSpecular = gl.createByteArray()
	lightSpecular.writeFloat32(1) 
	lightSpecular.writeFloat32(1) 
	lightSpecular.writeFloat32(1) 
	lightSpecular.writeFloat32(1)

	lightPosition = gl.createByteArray()
	lightPosition.writeFloat32(0) 
	lightPosition.writeFloat32(1) 
	lightPosition.writeFloat32(-1) 
	lightPosition.writeFloat32(1)

	gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, lightAmbient.getMemory())
	gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, lightDiffuse.getMemory())
	gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, lightSpecular.getMemory())
	gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, lightPosition.getMemory())
	gl.glEnable(gl.GL_LIGHT0)
}

function start()
{
	initData()
	gl.glutInit()
	gl.glutInitDisplayMode(gl.GLUT_DEPTH + gl.GLUT_DOUBLE + gl.GLUT_RGB + gl.GLUT_STENCIL)
	gl.glutInitWindowPosition(200, 200)
	gl.glutInitWindowSize(320, 320)
	gl.glutCreateWindow("cpgf OpenGL binding demo")
	gl.glutDisplayFunc(render)
	gl.glutReshapeFunc(reshape)
	gl.glutSpecialFunc(specialKey)
	    gl.glutKeyboardFunc(keyboard)
	period = 33
	gl.glutTimerFunc(period, timer, period)

	gl.glShadeModel(gl.GL_SMOOTH)
	    gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST)
	gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST)
	gl.glHint(gl.GL_POLYGON_SMOOTH_HINT, gl.GL_NICEST)

	    gl.glEnable(gl.GL_DEPTH_TEST)
	gl.glEnable(gl.GL_LIGHTING)
	gl.glEnable(gl.GL_TEXTURE_2D)
	gl.glEnable(gl.GL_CULL_FACE)

	gl.glColorMaterial(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT_AND_DIFFUSE)
	gl.glEnable(gl.GL_COLOR_MATERIAL)
	    gl.glClearColor(0, 0, 0, 0)
	gl.glClearStencil(0)
	gl.glClearDepth(1.0)
	gl.glDepthFunc(gl.GL_LEQUAL)

	initLights()
	gl.glutMainLoop()
}
